// packageI/ecologicalPoint/ecologicalPointOther/ecologicalPointOther.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
const app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    num_value: "",
    //受让人id
    UserId: "",
    tag: "",
    password: "",
    settingInfo: {
      point: 0,
      reward: false,
      transfer: false,
      fee_ratio: 0
    },
    show: false,
    ecologicalName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.status) {
      this.setData({
        tag: options.status
      })
    }
    this.setData({
      show: false,
      num_value: '',
    })
    this.getData();
  },



  allMethod(e) {
    this.setData({
      num_value: this.data.settingInfo.point
    })
  },
  checkNum(e) {
    let num = e.detail.value;
    this.setData({
      num_value: Number(num).toFixed(2)
    })
    if(Number(this.data.num_value) <= 0){
      this.setData({
        num_value : ''
      })
      return
    }
  },
  checkInp(e){
    let num = e.detail.value;
    this.setData({
      num_value: num
    })
  },
  setTitle() {
    let ecologicalName = this.data.ecologicalName;
    if (this.data.tag == 1) {
      wx.setNavigationBarTitle({
        title: `${ecologicalName}转让`,
      })
      return
    } else if (this.data.tag == 2) {
      wx.setNavigationBarTitle({
        title: `${ecologicalName}打赏`,
      })
      return
    } else if (this.data.tag == 3) {
      wx.setNavigationBarTitle({
        title: `提现${ecologicalName}`,
      })
      return
    }
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.ecological-point.frontend.modules.point.controllers.asset.index");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res.data)
          if (res.basic_info && res.basic_info.ecological_point) {
            this.setData({
              ecologicalName: res.basic_info.ecological_point ? res.basic_info.ecological_point : '生态积分'
            })
            this.setTitle()
          }
          this.setData({
            settingInfo: res.data
          })
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  bindUserId(e) {
    this.setData({
      UserId: e.detail.value
    })
  },
  getPostMethod() {
    if (this.data.tag == 1) {
      // 转让
      if (!this.data.num_value || this.data.num_value == 0) {
        app.tips("请输入转让数量");
        return;
      }
      if (!this.data.UserId) {
        app.tips("请输入受让人ID");
        return;
      }
      if (this.data.settingInfo.transfer) {
        this.setData({
          show: true,
          password: ""
        })
      } else {
        this.confirmMethod();
      }
    } else if (this.data.tag == 2) {
      // 打赏
      if (!this.data.num_value || this.data.num_value == 0) {
        Toast("请输入打赏数量");
        return;
      }
      if (!this.data.UserId) {
        Toast("请输入打赏人ID");
        return;
      }
      if (this.data.settingInfo.reward) {
        this.setData({
          show: true,
          password: ""
        })
      } else {
        this.confirmMethod();
      }
    } else {
      // 提现
      if (!this.data.num_value || this.data.num_value == 0) {
        Toast("请输入提现数量");
        return;
      }
      this.confirmMethod();
    }
  },
  passwordTap(e){
    this.setData({
      password:e.detail
    })
  },
  confirmMethod() {
    let url = "";
    let json = {};
    if (this.data.tag == 1 || this.data.tag == 2) {
      // 打赏 转让
      if (this.data.tag == 2) {
        url = "plugin.ecological-point.frontend.modules.point.controllers.reward.index";
      } else {
        url = "plugin.ecological-point.frontend.modules.point.controllers.transfer.index";
      }
      json = {
        amount: this.data.num_value,
        recipient_id: this.data.UserId,
        password: this.data.password
      };
    } else {
      // 提现
      url = "plugin.ecological-point.frontend.modules.point.controllers.withdraw.index";
      json.amount = this.data.num_value;
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          if (this.data.tag == 1 || this.data.tag == 2) {
            this.setData({
              password: "",
              UserId: "",
              num_value: ""
            })
          } else {
            this.setData({
              num_value: ""
            })
          }
          this.getData();
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let basic_info = wx.getStorageSync('yz_basic_info');
    this.setData({
      ecologicalName: basic_info.ecological_point ? basic_info.ecological_point : '生态积分'
    })
    this.setTitle()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})